#!/bin/bash

declare -i repeat=0;

while (( $# )) && [[ $1 =~ ^- ]]; do
    [[ $1 =~ ^(-r)|(--repeat)$ ]] && {
        let repeat++;
        shift; continue;
    }
    echo $0: unrecognized arguments: \'"$@"\' >&2;
    exit 1;
done


# If we are EDITING a shell script declare some cool stuff we can do.
[[ ${GEDIT_CURRENT_DOCUMENT_TYPE} = application/x-shellscript ]] && {
    
    # make some localized bash shortcuts.
    
    declare -A gedit[document]="${GEDIT_CURRENT_DOCUMENT_PATH}";
    
        gedit[directory]="${GEDIT_CURRENT_DOCUMENT_DIR}";
        gedit[word]="${GEDIT_CURRENT_WORD}";
        gedit[line]="${GEDIT_CURRENT_LINE_NUMBER}";
        gedit[name]="${GEDIT_CURRENT_DOCUMENT_NAME}";
        gedit[selection]="${GEDIT_SELECTED_TEXT}";
        
        # This is how we get gedit stuff in the Source Type list

        [[ -n ${gedit[selection]} ]] && {
            gedit[actions]="Selection|Source current selection|"
            function Selection() {
                eval ${zenusr[environment]} source \
                '<(echo -En "${gedit[selection]}")' ${zenusr[arguments]};
            }
        }
        
        gedit[actions]+='Command|Execute script|Script|Source current script|'
                
        gedit[command]="${gedit[document]}"; 
        
    function Command() {
        eval ${zenusr[environment]} '"${gedit[command]}"' ${zenusr[arguments]};
    }
    function Script() {
        eval ${zenusr[environment]} source '"${gedit[document]}"' \
        ${zenusr[arguments]};
    }

}

IFS='|,' read -a _zenusr < <(zenity --forms --text="Source" \
--add-list=Type \
--column-values="option|description" \
--list-values="${gedit[actions]}Scratch|Create a temporary script|Clipboard|Source the contents of the clipboard" \
--add-entry="Source Environment" \
--add-entry="Source Arguments" \
--title="Bash Executor" \
--width=600);

declare -A zenusr[0]="${_zenusr[0]}" zenusr[description]="${_zenusr[1]}"
zenusr[environment]="${_zenusr[2]}" zenusr[arguments]=${_zenusr[3]}
zenusr[title]= zenusr[width]=600 zenusr[height]=400;

unset _zenusr;

function Scratch() {
    
    local -r scratch=~/pid.$$.scratch.sh;
    
    touch $scratch;
    
    zenusr[title]="application/x-bashscript";
    
    zenusr[width]=600 zenusr[height]=400;
    
    eval ${zenusr[environment]:=false};
    
    while (( 1 )); do
    
        zenity --text-info --editable \
        --title="${zenusr[title]}" --filename=$scratch \
        --width="${zenusr[width]}" --height="${zenusr[height]}" >$scratch &&
        {
            eval source $scratch ${zenusr[arguments]}; \
            (( repeat )) \
                 || \
               break;
             continue;
        }
        
        break # ;)
        
    done
    
   # rm $scratch;
    
}

function Clipboard() {

    command -v xsel 2>&1 >/dev/null && {
        eval ${zenusr[environment]} source '<(xsel -o -b)' ${zenusr[arguments]};
        return;
    }
    
    zenity --text-info --html --title="Bash Executor Error" --width=500 --height=220 \
    --filename=<(echo "<body bgcolor=#f3f3f3><font size=2 color=black face=verdana>
    <h4><font color=orange>Unmet Command Dependencies</font></h4> " \
    "It looks like you do not have the xsel package installed. " \
    "Bash Executor requires this package to obtain clipboard data." \
    "<br>&nbsp;<hr> <table border=0><tr><td><font size=2 color=gray>" \
    "Press <b>Cancel</b> to abort this operation.</td>" \
    "<td align=right><font size=2 color=gray>" \
    "Press <b>OK</b> to open the scratch dialog.</td></tr></table>" \
    ) && Scratch;

}

eval $zenusr;

